package com.leps.nq.op.controller;

import cn.hutool.core.util.ObjectUtil;
import com.leps.nq.bu.entity.BuTeamCust;
import com.leps.nq.bu.entity.BuWell;
import com.leps.nq.bu.service.IBuTeamCustService;
import com.leps.nq.bu.service.IBuWellService;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.entity.OpBubbleReplenish;
import com.leps.nq.op.service.IOpBubbleReplenishService;
import com.leps.nq.op.service.IOpMonthlyTaskService;
import com.leps.nq.op.vo.BubbleReplenishQueryModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 泡排补液日志表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-06-22
 */
@RestController
@RequestMapping("/op/op-bubble-replenish")
@Api(tags = "泡排补液日志管理")
public class OpBubbleReplenishController {
    @Autowired
    IOpBubbleReplenishService bubbleReplenishService;
    @Autowired
    private IOpMonthlyTaskService monthlyTaskService;
    @Autowired
    private IBuWellService wellService;
    @Autowired
    private IBuTeamCustService teamCustService;

    @GetMapping
    @ApiOperation("泡排补液日志列表")
    public R<PageVo<OpBubbleReplenish>> list(BubbleReplenishQueryModel queryModel) {
        PageVo<OpBubbleReplenish> pageVo = this.bubbleReplenishService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询泡排补液信息")
    public R<OpBubbleReplenish> getById(@PathVariable Long id) {
        return RBuilder.ok(this.bubbleReplenishService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增泡排补液日志")
    @Transactional
    @Log(title = "泡排补液日志", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody OpBubbleReplenish bubbleReplenish) {
        // 补填队伍ID
        Integer custId = bubbleReplenish.getCustId();
        if(ObjectUtil.isNotNull(custId)) {
            BuTeamCust tc = this.teamCustService.lambdaQuery().eq(BuTeamCust::getCustId, custId).one();
            if(ObjectUtil.isNotNull(tc)) bubbleReplenish.setTeamId(tc.getTeamId());
        }
        this.bubbleReplenishService.save(bubbleReplenish);
        // 更新相关井最近一次补液信息
        updateLastBubbleReplenishInfoOfWell(bubbleReplenish);
        return RBuilder.ok(null);
    }

    @PutMapping
    @ApiOperation("修改泡排补液日志")
    @Transactional
    @Log(title = "泡排补液日志", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody OpBubbleReplenish bubbleReplenish) {
        // 补填队伍ID
        Integer custId = bubbleReplenish.getCustId();
        if(ObjectUtil.isNotNull(custId)) {
            BuTeamCust tc = this.teamCustService.lambdaQuery().eq(BuTeamCust::getCustId, custId).one();
            if(ObjectUtil.isNotNull(tc)) bubbleReplenish.setTeamId(tc.getTeamId());
        }
        this.bubbleReplenishService.updateById(bubbleReplenish);
        // 更新相关井最近一次补液信息
        updateLastBubbleReplenishInfoOfWell(bubbleReplenish);
        return RBuilder.ok();
    }

    private void updateLastBubbleReplenishInfoOfWell(OpBubbleReplenish bubbleReplenish) {
        BuWell well = new BuWell();
        well.setWellId(bubbleReplenish.getWellId());
        well.setLastReplenishTime(bubbleReplenish.getReplenishTime());
        this.wellService.updateById(well);
    }

    @DeleteMapping
    @ApiOperation("删除泡排补液日志")
    @Transactional
    @Log(title = "泡排补液日志", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Long> bubbleReplenishIds) {
        this.bubbleReplenishService.removeBatchByIds(bubbleReplenishIds);
        return RBuilder.ok();
    }
}
